#!/bin/bash
#### Usage: sh $0 <table-name>
usage ()
{
	echo "Usage: sh $0  <table-name>"
	echo "<table-name> - Provided table name will be truncated from msp database and will export table-name.sql in current directory."
}

tab_name="role_members"

mysqldump -hmsp-db -umsp -pmsp msp ${tab_name} > ${tab_name}.sql --single-transaction
ECODE1=$?
if [ $ECODE1 -ne 0 ]; then
	logger -t table_backup -p user.err "${tab_name} table backup failed for msp database - $ECODE1"
	echo "${tab_name} table backup failed for msp database"
	rm ${tab_name}
	exit 1
else
	logger -t table_backup -p user.err "msp database ${tab_name} table dump successfully completed"
	echo "msp database ${tab_name} table dump successfully"
fi 
mysql -hmsp-db -umsp -pmsp msp -e "TRUNCATE TABLE ${tab_name};"
ECODE2=$?
if [ $ECODE2 -ne 0 ]; then
	logger -t table_backup -p user.err "${tab_name} table truncate failed - $ECODE1"
	echo "${tab_name} table truncate failed."
	exit 1
else
	logger -t table_backup -p user.err "${tab_name} table truncate successfully completed"
	echo "${tab_name} table truncate successfully completed"
fi
